import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/values/constant.dart';
import 'package:xin_nuo_pda/views/SubTitleBar.dart';
import 'package:xin_nuo_pda/views/TextView.dart';
import 'package:xin_nuo_pda/views/edittext/ClearEditText.dart';
import 'package:xin_nuo_pda/utils/string_extension.dart';
import 'package:xin_nuo_pda/values/colors.dart';
import '../../bean/work_load_list_entity.dart';
import '../../views/edittext/TitleEditText.dart';
import 'PassEndWorkNextOneController.dart';

class PassEndWorkNextOnePage extends StatelessWidget {
  final controller = Get.put(PassEndWorkNextOneController());
  final state = Get.find<PassEndWorkNextOneController>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(USER),
        ),
        body: SingleChildScrollView(
          child:  Container(
              child: Column(
                children: [
                  SubTitleBar("编带出站", '提交', () {
                    controller.trackOut();
                  }),
                  Container(
                    margin: EdgeInsets.all(8),
                    alignment: Alignment.centerLeft,
                    child: Text(
                      "出站信息",
                      style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                    ),
                  ),
                  TextView("批号:", state.lotEntity?.lOTID ?? ''),
                  TextView("BIN名称:", state.binBean?.BINNAME ?? ''),
                  TextView("数量:", state.binBean!.MAINQTY.toString()),
                  TextView("卷盘基数:", state.lotEntity?.REELPACKNUM ?? ''),
                  Container(
                    margin: EdgeInsets.all(8),
                    alignment: Alignment.centerLeft,
                    child: Text(
                      "载具列表",
                      style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                    ),
                  ),
                  ClearEditText("请扫描载具ID", (value) {
                    controller.addCarryData(value);
                  }),
                  SizedBox(
                    height: 4,
                  ),
                  GetBuilder<PassEndWorkNextOneController>(
                      builder: (logic) => Table(
                        defaultVerticalAlignment:
                        TableCellVerticalAlignment.middle,
                        columnWidths: {
                          //列宽
                          0: FlexColumnWidth(2.0),
                          1: FlexColumnWidth(8.0),
                        },
                        border: TableBorder.all(
                          color: Colors.grey,
                          width: 1.0,
                          style: BorderStyle.solid,
                        ),
                        children: getCarryList(context),
                      ),
                      id: state.carryLogicId),
                  Container(
                    margin: EdgeInsets.all(8),
                    alignment: Alignment.centerLeft,
                    child: Text(
                      "添加工作量",
                      style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                    ),
                  ),
                  SizedBox(
                    height: 4,
                  ),
                  GetBuilder<PassEndWorkNextOneController>(
                    builder: (logic) => Align(
                      alignment: Alignment.center,
                      child: Row(
                        children: [
                          SizedBox(
                            width: 12,
                          ),
                          Container(
                            height: 36,
                            margin: EdgeInsets.only(right: 8),
                            child: ElevatedButton(
                              onPressed: () {
                                controller.addWork();
                              },
                              child: Text("添加"),
                            ),
                          ),
                          SizedBox(
                            width: 4,
                          ),
                        ],
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 4,
                  ),
                  GetBuilder<PassEndWorkNextOneController>(
                    builder: (logic) => Table(
                      defaultVerticalAlignment:
                      TableCellVerticalAlignment.middle,
                      columnWidths: {
                        //列宽
                        0: FlexColumnWidth(2.0),
                        1: FlexColumnWidth(1.0),
                        2: FlexColumnWidth(2.0),
                      },
                      border: TableBorder.all(
                        color: Colors.grey,
                        width: 1.0,
                        style: BorderStyle.solid,
                      ),
                      children: getDefectList(context),
                    ), id: controller.listID,
                  ),
                  TitleEditText(
                    "机台数量:",
                    "请输入机台数量",
                        (value) {
                    },
                    controller: controller.controller,
                  ),
                ],
              ),
            ),
          ),
        );
  }

  List<TableRow> getDefectList(BuildContext context) {
    List<TableRow> tempView = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '批次号',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '工号',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '数量',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < controller.defectList.length; i++) {
      WorkLoadListEntity element = controller.defectList[i];
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              element.lotId ?? '',
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: ClearEditText(
            "",
                (value) {
              element.userName = value;
            },
            isScan: false,isShowIcon: false,
            controller: TextEditingController(text: element.userName ?? ''),
          ),
        ),
        Align(
          alignment: Alignment.topCenter,
          child: Container(
            padding: EdgeInsets.only(bottom: 4),
            child: ClearEditText(
              "",
                  (value) {
                if (value.isEmpty) {
                  value = "0";
                }
                element.mainQty = value.toDouble();
              },
              type: TextInputType.number,
              isScan: false,isShowIcon: false,
              controller: TextEditingController(text: element.mainQty == null ? '0' : element.mainQty.toString()),
            ),
          ),
        )
      ]));
    }
    return tempView;
  }


  List<TableRow> getCarryList(BuildContext context) {
    List<TableRow> tempView = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '序号',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '载具编号',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < state.carryList.length; i++) {
      String element = state.carryList[i];
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              "${i + 1}",
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Row(
            children: [
              SizedBox(
                width: 8,
              ),
              Expanded(
                  flex: 1,
                  child: Text(
                    element,
                    style: TextStyle(
                      color: text_black.toColor(),
                    ),
                  )),
              IconButton(
                icon: Icon(
                  Icons.delete,
                  color: Colors.red,
                ),
                onPressed: () {
                  controller.deleteCarryData(element);
                },
              )
            ],
          ),
        ),
      ]));
    }
    return tempView;
  }
}
